package com.dsx.common.bean;


import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * 客户端响应对象
 *
 * @author tianwenqing
 */
public class Response<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public static final String OK = "ok";

    public static final String ERROR = "error";

    private T data; //返回信息

    private boolean success; //是否成功标志

    private String message; //描述信息

    private int code; //状态码

    @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date nowTime = new Date();//系统当前时间

    public Response() {
    }

    public Response(T data, boolean success, String message, int code, Date nowTime) {
        this.data = data;
        this.success = success;
        this.message = message;
        this.code = code;
        this.nowTime = nowTime;
    }

    public Response success() {
        this.success = true;
        this.message = OK;
        this.code = ResponseCodeEnum.SUCCESS.getCode();
        return this;
    }

    public Response success(T data) {
        this.success = true;
        this.message = OK;
        this.data = data;
        this.code = ResponseCodeEnum.SUCCESS.getCode();
        return this;
    }

    public Response failure() {
        this.success = false;
        this.message = ERROR;
        this.code = ResponseCodeEnum.FAIL_BUSINESS.getCode();
        return this;
    }

    public Response failure(ResponseCodeEnum responseCodeEnum) {
        this.success = false;
        this.message = ERROR;
        this.code = responseCodeEnum.getCode();
        return this;
    }

    public Response failure(String message) {
        this.success = false;
        this.message = message;
        this.code = ResponseCodeEnum.FAIL_BUSINESS.getCode();
        return this;
    }

    public Response failure(String message, ResponseCodeEnum responseCodeEnum) {
        this.success = false;
        this.message = message;
        this.code = responseCodeEnum.getCode();
        return this;
    }

    public Response failure(String message, T data) {
        this.success = false;
        this.message = message;
        this.data = data;
        this.code = ResponseCodeEnum.FAIL_BUSINESS.getCode();
        return this;
    }


    public Response failure(String message, T data, ResponseCodeEnum responseCodeEnum) {
        this.success = false;
        this.message = message;
        this.data = data;
        this.code = responseCodeEnum.getCode();
        return this;
    }


    public T getData() {
        return data;
    }


    public boolean isSuccess() {
        return success;
    }


    public void setSuccess(boolean success) {
        this.success = success;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    public void setData(T data) {
        this.data = data;
    }

    public Date getNowTime() {
        return nowTime;
    }

    public void setNowTime(Date nowTime) {
        this.nowTime = nowTime;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
